cmake_minimum_required (VERSION 2.8.1)

PROJECT(libdbusglue)

SET( ${PROJECT_NAME}_MAJOR_VERSION 3 )
SET( ${PROJECT_NAME}_MINOR_VERSION 1 )
SET( ${PROJECT_NAME}_PATCH_LEVEL 12 )

message("Project name: ${PROJECT_NAME}")

SET(PACKAGE_VERSION "${${PROJECT_NAME}_MAJOR_VERSION}.${${PROJECT_NAME}_MINOR_VERSION}.${${PROJECT_NAME}_PATCH_LEVEL}")
SET(COMMONAPI_API_HEADER_VERSION ${${PROJECT_NAME}_MAJOR_VERSION}.${${PROJECT_NAME}_MINOR_VERSION}) # used in *.cmake.in

##############################################################################

# create the commandline variables:
OPTION(BUILD_SHARED_LIBS "Set to OFF to build static libraries" ON )
message("BUILD_SHARED_LIBS is set to value: ${BUILD_SHARED_LIBS}")

OPTION(USE_INSTALLED_COMMONAPI "Set to OFF to use the local (build tree) version of CommonAPI" ON)
message("USE_INSTALLED_COMMONAPI is set to value: ${USE_INSTALLED_COMMONAPI}")

OPTION(USE_INSTALLED_DBUS "Set to OFF to use the local (build tree) version of DBUS" ON)
message(STATUS "USE_INSTALLED_DBUS is set to value: ${USE_INSTALLED_DBUS}")

OPTION(USE_FILE "Set to OFF to disable file logging" OFF )
message(STATUS "USE_FILE is set to value: ${USE_FILE}")

OPTION(USE_CONSOLE "Set to OFF to disable console logging" OFF )
message(STATUS "USE_CONSOLE is set to value: ${USE_CONSOLE}")

if(NOT CMAKE_BUILD_TYPE)
   set(CMAKE_BUILD_TYPE "Debug" CACHE STRING
       "Choose the type of build, options are: Debug Release." FORCE)
endif(NOT CMAKE_BUILD_TYPE)

SET(COMMONAPI_CMAKE_INSTALL_PATH "na" CACHE STRING "CommonAPI install path of the cmake files")
SET(COMMONAPI_DBUS_CMAKE_INSTALL_PATH "na" CACHE STRING "CommonAPI-DBus install path of the cmake files")
SET(COMMONAPI_DBUS_TOOL_GENERATOR "na" CACHE STRING "CommonAPI-DBus-Tools generator install path")
SET(COMMONAPI_TEST_FIDL_PATH "na" CACHE STRING "Path to directory with test fidl files for code generation")
SET(COMMONAPI_SRC_GEN_DEST "src-gen" CACHE STRING "Path to the destination directory of the generated sources")

if("${COMMONAPI_DBUS_TOOL_GENERATOR}" STREQUAL "na")
    message(FATAL_ERROR "The file path for the commonapi_dbus_generator needs to be specified! Use '-DCOMMONAPI_DBUS_TOOL_GENERATOR' to do so.")
    return()
else()
    message("COMMONAPI_DBUS_TOOL_GENERATOR specified: ${COMMONAPI_DBUS_TOOL_GENERATOR}")
endif()

if("${COMMONAPI_TOOL_GENERATOR}" STREQUAL "na")
    message(FATAL_ERROR "The file path for the commonapi_dbus_generator needs to be specified! Use '-DCOMMONAPI_TOOL_GENERATOR' to do so.")
    return()
else()
    message("COMMONAPI_TOOL_GENERATOR specified: ${COMMONAPI_TOOL_GENERATOR}")
endif()
message("Build type: ${CMAKE_BUILD_TYPE}")

message("The path to the destination directory of the generated sources is set to: ${COMMONAPI_SRC_GEN_DEST}")

##############################################################################
# add COMMONAPI install path to cmake find root path

if(NOT "${COMMONAPI_CMAKE_INSTALL_PATH}" STREQUAL "na")
  set( CMAKE_FIND_ROOT_PATH ${COMMONAPI_CMAKE_INSTALL_PATH};${CMAKE_FIND_ROOT_PATH} )
endif()
if(NOT "${COMMONAPI_DBUS_CMAKE_INSTALL_PATH}" STREQUAL "na")
  set( CMAKE_FIND_ROOT_PATH ${COMMONAPI_DBUS_CMAKE_INSTALL_PATH};${CMAKE_FIND_ROOT_PATH} )
endif()
message("CMAKE_FIND_ROOT_PATH: ${CMAKE_FIND_ROOT_PATH}")

FIND_PACKAGE(PkgConfig)

if ("${USE_INSTALLED_COMMONAPI}" STREQUAL "ON")
    FIND_PACKAGE(CommonAPI REQUIRED CONFIG NO_CMAKE_PACKAGE_REGISTRY)
    FIND_PACKAGE(CommonAPI-DBus REQUIRED CONFIG NO_CMAKE_PACKAGE_REGISTRY)
else()
    FIND_PACKAGE(CommonAPI REQUIRED CONFIG NO_SYSTEM_ENVIRONMENT_PATH NO_CMAKE_SYSTEM_PATH)
    FIND_PACKAGE(CommonAPI-DBus REQUIRED CONFIG NO_SYSTEM_ENVIRONMENT_PATH NO_CMAKE_SYSTEM_PATH)
endif()

message("CommonAPI_CONSIDERED_CONFIGS: ${CommonAPI_CONSIDERED_CONFIGS}")
message("COMMONAPI_INCLUDE_DIRS: ${COMMONAPI_INCLUDE_DIRS}")
message("CommonAPI-DBus_CONSIDERED_CONFIGS: ${CommonAPI-DBus_CONSIDERED_CONFIGS}")
message("COMMONAPI_DBUS_INCLUDE_DIRS: ${COMMONAPI_DBUS_INCLUDE_DIRS}")

###############################################################################
# find DBus by using the 'pkg-config' tool
# CommonAPI
include(FindPkgConfig)
###############################################################################
# find DBus by using the 'pkg-config' tool
if (MSVC)
    #Not beautiful, but it works
    if (DBus_DIR)
        if (DBus_BUILD_DIR)
            set(DBus_INCLUDE_DIRS "${DBus_DIR};${DBus_BUILD_DIR};")
        else ()
            message (FATAL_ERROR "DBus_BUILD_DIR not set! Cannot continue.")
        endif ()
    else()
        message (FATAL_ERROR "DBus_DIR not set! Cannot continue.")
    endif ()
else()
    pkg_check_modules(DBus REQUIRED dbus-1>=1.4)
endif()

##############################################################################
# generate code from fidl
file(GLOB FIDL_FILES "${COMMONAPI_TEST_FIDL_PATH}/*.fidl")
message("FIDL_FILES: ${FIDL_FILES}")
execute_process(COMMAND ${COMMONAPI_DBUS_TOOL_GENERATOR} -dest ${COMMONAPI_SRC_GEN_DEST} ${FIDL_FILES}
                        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
                        )
execute_process(COMMAND ${COMMONAPI_TOOL_GENERATOR} -dest ${COMMONAPI_SRC_GEN_DEST} ${FIDL_FILES}
                        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
                        )
##############################################################################
# dbus glue build section

IF(USE_FILE)
  add_definitions(-DUSE_FILE)
ENDIF(USE_FILE)
IF(USE_CONSOLE)
  add_definitions(-DUSE_CONSOLE)
ENDIF(USE_CONSOLE)

SET(MAX_LOG_LEVEL "DEBUG" CACHE STRING "maximum log level")
message(STATUS "MAX_LOG_LEVEL is set to value: ${MAX_LOG_LEVEL}")
add_definitions(-DCOMMONAPI_LOGLEVEL=COMMONAPI_LOGLEVEL_${MAX_LOG_LEVEL})

if (MSVC)
# Visual C++ is not always sure whether he is really C++
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_CRT_SECURE_NO_WARNINGS /EHsc -DCOMMONAPI_INTERNAL_COMPILATION /wd\\\"4503\\\"")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_CRT_SECURE_NO_WARNINGS -DCOMMONAPI_INTERNAL_COMPILATION /wd\\\"4503\\\"")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -Wall -Wextra -Wformat -Wformat-security -Wconversion -fexceptions -fstrict-aliasing -fstack-protector -fasynchronous-unwind-tables -fno-omit-frame-pointer -D_GLIBCXX_USE_NANOSLEEP")
endif()

message("Compiler options: ${CMAKE_CXX_FLAGS}")

include_directories(
    src
    ${COMMONAPI_SRC_GEN_DEST}
    ${COMMONAPI_INCLUDE_DIRS}
    ${DBus_INCLUDE_DIRS}
    ${COMMONAPI_DBUS_INCLUDE_DIRS}
)

if ("${USE_INSTALLED_DBUS}" STREQUAL "ON")
    link_directories(
        ${DBus_LIBRARY_DIRS}
    )
else()
    link_directories(
        ${DBus_INCLUDE_DIRS}/dbus/.libs
    )
endif()

if (MSVC)
    # source files
    file(GLOB GLUE_SRCS "${COMMONAPI_SRC_GEN_DEST}/v1/commonapi/runtime/*.cpp"
        "${COMMONAPI_SRC_GEN_DEST}/v1/commonapi/communication/*.cpp"
        "${COMMONAPI_SRC_GEN_DEST}/v1/commonapi/advanced/bselective/*.cpp"
        "${COMMONAPI_SRC_GEN_DEST}/v1/commonapi/advanced/managed/*.cpp"
        "${COMMONAPI_SRC_GEN_DEST}/v1/commonapi/advanced/extended/*.cpp"
        "${COMMONAPI_SRC_GEN_DEST}/v1/commonapi/advanced/polymorph/*.cpp"
        "${COMMONAPI_SRC_GEN_DEST}/v1/commonapi/datatypes/primitive/*.cpp"
        "${COMMONAPI_SRC_GEN_DEST}/v1/commonapi/datatypes/advanced/*.cpp"
        "${COMMONAPI_SRC_GEN_DEST}/v1/commonapi/datatypes/derived/*.cpp"
        "${COMMONAPI_SRC_GEN_DEST}/v1/commonapi/datatypes/combined/*.cpp"
        "${COMMONAPI_SRC_GEN_DEST}/v1/commonapi/threading/*.cpp"
        "${COMMONAPI_SRC_GEN_DEST}/v1/commonapi/performance/primitive/*.cpp"
        "${COMMONAPI_SRC_GEN_DEST}/v1/commonapi/performance/complex/*.cpp"
        "${COMMONAPI_SRC_GEN_DEST}/v1/commonapi/stability/sp/*.cpp")
else()
    # source files
    file(GLOB GLUE_SRCS "${COMMONAPI_SRC_GEN_DEST}/v1/commonapi/runtime/*DBus*.cpp"
        "${COMMONAPI_SRC_GEN_DEST}/v1/commonapi/communication/*DBus*.cpp"
        "${COMMONAPI_SRC_GEN_DEST}/v1/commonapi/advanced/bselective/*DBus*.cpp"
        "${COMMONAPI_SRC_GEN_DEST}/v1/commonapi/advanced/managed/*DBus*.cpp"
        "${COMMONAPI_SRC_GEN_DEST}/v1/commonapi/advanced/extended/*DBus*.cpp"
        "${COMMONAPI_SRC_GEN_DEST}/v1/commonapi/advanced/polymorph/*DBus*.cpp"
        "${COMMONAPI_SRC_GEN_DEST}/v1/commonapi/datatypes/primitive/*DBus*.cpp"
        "${COMMONAPI_SRC_GEN_DEST}/v1/commonapi/datatypes/advanced/*DBus*.cpp"
        "${COMMONAPI_SRC_GEN_DEST}/v1/commonapi/datatypes/derived/*DBus*.cpp"
        "${COMMONAPI_SRC_GEN_DEST}/v1/commonapi/datatypes/combined/*DBus*.cpp"
        "${COMMONAPI_SRC_GEN_DEST}/v1/commonapi/threading/*DBus*.cpp"
        "${COMMONAPI_SRC_GEN_DEST}/v1/commonapi/performance/primitive/*DBus*.cpp"
        "${COMMONAPI_SRC_GEN_DEST}/v1/commonapi/performance/complex/*DBus*.cpp"
        "${COMMONAPI_SRC_GEN_DEST}/v1/commonapi/stability/sp/*DBus*.cpp")
endif()

# CommonAPI-DBus library
add_library(DBusGlue ${GLUE_SRCS})
target_link_libraries(DBusGlue CommonAPI-DBus)

set_target_properties(DBusGlue PROPERTIES VERSION 0.0.0 SOVERSION 0 LINKER_LANGUAGE C)

##############################################################################
# exporting, configuring and installing of cmake files

# Add all targets to the build-tree export set
export(TARGETS DBusGlue
  FILE "${PROJECT_BINARY_DIR}/DBusGlueTargets.cmake")

# Export the package for use from the build-tree
# (this registers the build-tree with a global CMake-registry)
export(PACKAGE DBusGlue)

# Create the DBusGlueConfig.cmake and DBusGlueConfigVersion files ...
# ... for the build tree
set(CONF_INCLUDE_DIRS "${PROJECT_SOURCE_DIR}/src")
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/DBusGlueConfig.cmake.in
  "${PROJECT_BINARY_DIR}/DBusGlueConfig.cmake" @ONLY)

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/DBusGlueConfigVersion.cmake.in
  "${PROJECT_BINARY_DIR}/DBusGlueConfigVersion.cmake" @ONLY)

##############################################################################
# maintainer-clean
add_custom_target(maintainer-clean COMMAND rm -rf *)
